package cn.iocoder.yudao.module.ym.dal.mysql.ticket;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.ym.dal.dataobject.ticket.TicketDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.*;
import cn.iocoder.yudao.module.ym.controller.admin.ticket.vo.*;

/**
 * 工单 Mapper
 *
 * @author 韩艳阳
 */
@Mapper
public interface TicketMapper extends BaseMapperX<TicketDO> {

    default PageResult<TicketDO> selectPage(TicketPageReqVO reqVO) {
        LambdaQueryWrapper<TicketDO> lambdaQueryWrapper = new LambdaQueryWrapperX<TicketDO>()
                .eqIfPresent(TicketDO::getCreator, reqVO.getCreator())
                .inIfPresent(TicketDO::getSiteId, reqVO.getSiteIds())
                .inIfPresent(TicketDO::getAreaId, reqVO.getAreaIds())
                .betweenIfPresent(TicketDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(TicketDO::getStatus, reqVO.getStatus())
                .likeIfPresent(TicketDO::getRemark, reqVO.getRemark())
                .likeIfPresent(TicketDO::getChargingPileId, reqVO.getChargingPileId())
                .likeIfPresent(TicketDO::getTicketDesc, reqVO.getTicketDesc())
                .eqIfPresent(TicketDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(TicketDO::getThirdId, reqVO.getThirdId())
                .likeIfPresent(TicketDO::getTicketNumber, reqVO.getTicketNumber())
                .eqIfPresent(TicketDO::getTicketType, reqVO.getTicketType())
                .eqIfPresent(TicketDO::getTicketLevel, reqVO.getTicketLevel())
                .eqIfPresent(TicketDO::getTicketStatus, reqVO.getTicketStatus())
                .eqIfPresent(TicketDO::getAreaId, reqVO.getAreaId())
                .eqIfPresent(TicketDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(TicketDO::getOperatorId, reqVO.getOperatorId())
                .inIfPresent(TicketDO::getOperatorId, reqVO.getOperatorIdList())
                .betweenIfPresent(TicketDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(TicketDO::getEndTime, reqVO.getEndTime())
                .likeIfPresent(TicketDO::getProcessing, reqVO.getProcessing())
                .likeIfPresent(TicketDO::getFaultDesc, reqVO.getFaultDesc())
                .eqIfPresent(TicketDO::getFaultImage, reqVO.getFaultImage())
                .last("ORDER BY " +
                        "FIELD(ticket_status, 2) DESC, " +  // 状态2优先
                        "ticket_status ASC, " +            // 其他状态升序
                        "end_time DESC, " +                // 结束时间倒序
                        "id DESC"                          // ID倒序
                );

        return selectPage(reqVO, lambdaQueryWrapper);

    }

    default List<TicketDO> selectList(TicketPageReqVO reqVO) {

        LambdaQueryWrapper<TicketDO> ticketDOLambdaQueryWrapperX = new LambdaQueryWrapperX<TicketDO>()
                .inIfPresent(TicketDO::getSiteId, reqVO.getSiteIds())
                .inIfPresent(TicketDO::getAreaId, reqVO.getAreaIds())
                .betweenIfPresent(TicketDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(TicketDO::getStatus, reqVO.getStatus())
                .likeIfPresent(TicketDO::getRemark, reqVO.getRemark())
                .likeIfPresent(TicketDO::getChargingPileId, reqVO.getChargingPileId())
                .likeIfPresent(TicketDO::getTicketDesc, reqVO.getTicketDesc())
                .eqIfPresent(TicketDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(TicketDO::getThirdId, reqVO.getThirdId())
                .likeIfPresent(TicketDO::getTicketNumber, reqVO.getTicketNumber())
                .eqIfPresent(TicketDO::getTicketType, reqVO.getTicketType())
                .eqIfPresent(TicketDO::getTicketLevel, reqVO.getTicketLevel())
                .eqIfPresent(TicketDO::getTicketStatus, reqVO.getTicketStatus())
                .eqIfPresent(TicketDO::getAreaId, reqVO.getAreaId())
                .eqIfPresent(TicketDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(TicketDO::getOperatorId, reqVO.getOperatorId())
                .inIfPresent(TicketDO::getOperatorId, reqVO.getOperatorIdList())
                .betweenIfPresent(TicketDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(TicketDO::getEndTime, reqVO.getEndTime())
                .likeIfPresent(TicketDO::getProcessing, reqVO.getProcessing())
                .likeIfPresent(TicketDO::getFaultDesc, reqVO.getFaultDesc())
                .eqIfPresent(TicketDO::getFaultImage, reqVO.getFaultImage())
                .last("ORDER BY " +
                        "FIELD(ticket_status, 2) DESC, " +  // 状态2优先
                        "ticket_status ASC, " +            // 其他状态升序
                        "end_time DESC, " +                // 结束时间倒序
                        "id DESC"                          // ID倒序
                );


        return selectList(ticketDOLambdaQueryWrapperX);


    }

    @Select("SELECT operator_id, COUNT(1) AS count FROM ym_ticket GROUP BY operator_id")
    List<Map<Long, Object>> allCount();

    @Select("SELECT operator_id, COUNT(1) AS `count` FROM ym_ticket WHERE DATE(end_time) = CURDATE()    AND deleted = 0 GROUP BY operator_id")
    List<Map<Long, Object>> todayCount();


    //根据工单创建时间
    @Select("SELECT DATE(create_time) AS createDate, COUNT(id) AS dailyCount " +
            "FROM ym_ticket " +
            "WHERE operator_id = #{operatorId} and end_time is not null" +
            "GROUP BY DATE(create_time) " +
            "ORDER BY createDate DESC " +
            "LIMIT #{pageNo}, #{pageSize}")
    List<Map<String, Object>> getDailyTicketCountByOperator(@Param("operatorId") Long operatorId,
                                                            @Param("pageNo") Integer pageNo,
                                                            @Param("pageSize") Integer pageSize);

    //根据工单完成时间
    @Select("SELECT DATE(end_time) AS endDate, COUNT(id) AS dailyCount " +
            "FROM ym_ticket " +
            "WHERE operator_id = #{operatorId} and ticket_status  = 3  " +
            "GROUP BY DATE(end_time) " +
            "ORDER BY endDate DESC " +
            "LIMIT #{pageNo}, #{pageSize}")
    List<Map<String, Object>> getDailyTicketCountByOperatorEnd(@Param("operatorId") Long operatorId,
                                                            @Param("pageNo") Integer pageNo,
                                                            @Param("pageSize") Integer pageSize);


    // todo 明天改回来，去调用对应方法，暂时这样
    //不分页用来获取total ,根据工单创建时间
    @Select("SELECT DATE(end_time) AS endDate, COUNT(id) AS dailyCount " +
            "FROM ym_ticket " +
            "WHERE operator_id = #{operatorId} and ticket_status  = 3  " +
            "GROUP BY DATE(end_time) " +
            "ORDER BY endDate DESC ")
    List<Map<String, Object>> getDailyTicketCountByOperatorList(@Param("operatorId") Long operatorId);


    //不分页用来获取total，根据完单时间
    @Select("SELECT DATE(end_time) AS endDate, COUNT(id) AS dailyCount " +
            "FROM ym_ticket " +
            "WHERE operator_id = #{operatorId} and ticket_status  = 3  " +
            "GROUP BY DATE(end_time) " +
            "ORDER BY endDate DESC ")
    List<Map<String, Object>> getDailyTicketCountByOperatorListEnd(@Param("operatorId") Long operatorId);



  /*  //获取每日不同类型的工单数量
    @Select("SELECT\n" +
            "COUNT(IF(ticket_type = 1, 1, NULL)) AS xunjian,\n" +
            "COUNT(IF(ticket_type = 2, 1, NULL)) AS baoyang,\n" +
            "COUNT(IF(ticket_type = 3, 1, NULL)) AS weixiu\n" +
            "FROM ym_ticket\n" +
            "WHERE deleted = 0 " +
            "AND operator_id = #{operatorId} \n" +
            "AND create_time >= #{sTime} \n" +
            "  AND create_time < #{sTime} + INTERVAL 1 DAY")
    Map<String, Object> ticketTypeCount(@Param("sTime") LocalDate sTime , @Param("operatorId") Long operatorId);*/


}
